package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//获取 form 表单提交的参数

func main() {
	r := gin.Default()
	r.LoadHTMLFiles("web12_post/login.html", "web12_post/index.html")

	//一次请求对应一个响应！！！
	r.GET("/login", func(context *gin.Context) {
		context.HTML(http.StatusOK, "login.html", nil)
	})

	// /login post
	r.POST("/login", func(context *gin.Context) {
		//获取 form 表单提交的数据
		//方式1：
		username := context.PostForm("username")
		password := context.PostForm("password") //取到就返回值，取不到就返回空字符串
		//方式2：
		username = context.DefaultPostForm("username", "somebody")
		password = context.DefaultPostForm("password", "***")
		//方式3：
		ok := false
		username, ok = context.GetPostForm("username")
		if !ok {
			username = "sb"
		}
		password, ok = context.GetPostForm("password")
		if !ok {
			password = "***"
		}
		context.HTML(http.StatusOK, "index.html", gin.H{
			"Name":     username,
			"Password": password,
		})
	})

	r.Run(":9090")
}
